<script setup lang="ts">
import Write from './components/Write.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { useI18n } from '@/hooks/web/useI18n'
import { useEventBus } from '@/hooks/event/useEventBus'
import { saveEditApi } from '@/api/inventory'

const { emit } = useEventBus()

const { push, go } = useRouter()

const { t } = useI18n()
const { query } = useRoute()

const writeRef = ref()

const loading = ref(false)

const save = async () => {
  const write = unref(writeRef)
  const formData = await write?.submit()

  const params = {
    id: parseInt(query.id as string),
    is_sale: formData.is_sale,
    allow_zero_stock_sale: formData.allow_zero_stock_sale,
    product_prices: formData.gradientList,
    ccname_ids: formData.ccname_ids,
    remark: formData.notes,
    real_arrival_date: formData.real_arrival_date // 2025-07-14 新增修改真实到货日期
  }

  if (params) {
    loading.value = true
    const res = await saveEditApi(params)
      .catch(() => {})
      .finally(() => {
        loading.value = false
      })
    if (res) {
      emit('getList', 'editor')
      await push('/warehouse/inventory-management')
    }
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('product.edit')" @back="push('/product')">
    <Write ref="writeRef" :index="2" />

    <template #header>
      <BaseButton @click="go(-1)">
        {{ t('common.back') }}
      </BaseButton>
      <BaseButton type="primary" :loading="loading" @click="save">
        {{ t('action.save') }}
      </BaseButton>
    </template>
  </ContentDetailWrap>
</template>
@/hooks/event/useEventBus
